/**
 * 自定义touch指令，该指令会忽略拖动动作
 */
var _bindFun_ = function (el, binding, vnode) {
  if (!vnode || !binding.value || !binding.value.IsFunction) {
    return
  }
  var touchmove = false
  vnode._touchEvents = {
    touchstart() {
      touchmove = false
    },
    touchmove () {
      touchmove = true
    },
    touchend() {
      if (touchmove) {
        touchmove = false
        return
      }
      binding.value.call(vnode.context, vnode.data.attrs, vnode, el)
    }
  }
  Object.keys(vnode._touchEvents).forEach(key => {
    el.addEventListener(key, vnode._touchEvents[key])
  })
}

var _unBindFun_ = function(el, binding, vnode) {
  if (!vnode) {
    return
  }
  if (vnode._touchEvents) {
    Object.keys(vnode._touchEvents).forEach(key => {
      el.removeEventListener(key, vnode._touchEvents[key])
    })
  }
  delete vnode._touchEvents
}

Vue.directive('touch', {
  bind: (el, binding, vnode) => {
    _bindFun_(el, binding, vnode)
  },
  update: (el, binding, vnode) => {
    _unBindFun_(el, binding, vnode)
    _bindFun_(el, binding)
  },
  unbind: (el, binding, vnode) => {
    _unBindFun_(el, binding, vnode)
  }
})
